<template>
  <div class='sub-category' v-if="subCate">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选数据 -->
      <SubFilter></SubFilter>
       <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort />
        <!-- 加载 -->
        <XtxInfiniteLoading v-model="loading" :finished='finished' @load='goodsList'   finished-text="没有更多了">
        <!-- 商品列表 -->
        <ul class="list">
          <li v-for="item in list" :key="item.id">
            <GoodsItem :goods='item'></GoodsItem>
          </li>
        </ul>
           </XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import { findSubCategory, findSubCategoryGoods } from '@/api/category'
import SubBread from './components/sub-bread'
import { useRoute } from 'vue-router'
import { watch, ref, provide, reactive } from 'vue'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import XtxInfiniteLoading from '@/components/library/xtx-infinite-loading.vue'

export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem, XtxInfiniteLoading },
  setup () {
    const route = useRoute()
    const subCate = ref(null)

    // 请求参数
    let data = {
      page: 1, // 页码
      pageSize: 20 // 页容量
    }
    // 加载状态
    const loading = ref(false)
    const finished = ref(false)
    // 因为vue对象指向同一地址，所有不会更新 ，写成函数
    const defaultParams = () => {
      return {
        categoryId: route.params.id, // 分类id
        // 排序字段 null
        sortField: null, // [publishTime,orderNum,price,evaluateNum]
        //   价格排序规则
        sortMethod: undefined, // asc为正序 desc为倒序  默认desc
        inventory: false, // 是否有库存
        onlyDiscount: false, // 只显示特惠
        attrs: [], // 属性条件数组[{groupName:xxx,propertyName:xxx}]
        brandId: null // 品牌id
        // page: 1, // 页码
        // pageSize: 20// 页码条数
      }
    }
    // 监听路由id发生变化
    watch(() => route.params.id, async (id) => {
      if (route.path !== `/category/sub/${id}`) return
      const { result } = await findSubCategory(id)
      // console.log(result)
      subCate.value = result

      // 让排序组件恢复默认值
      // sortParams.sortField = null
      // sortParams.sortMethod = null
      // sortParams.inventory = false
      // sortParams.onlyDiscount = false
      const obj = defaultParams()
      for (const k in obj) {
        sortParams[k] = obj[k]
      }
    }, { immediate: true })
    provide('subCate', subCate)
    // console.log(subCate)
    // 用于商品列表--分页查询接口的查询参数
    const sortParams = reactive(defaultParams())
    // 导出
    provide('sortParams', sortParams)

    // 商品列表
    const list = ref([])
    const goodsList = async () => {
      // 发送请求前  先设置为true
      loading.value = true
      // 有筛选功能
      // const { result } = await findSubCategoryGoods(sortParams)
      const { result } = await findSubCategoryGoods(data)
      // 没有筛选功能
      // const { result } = await findSubCategoryGoodsTemp(sortParams)
      // 请求完  设置为false
      loading.value = false
      console.log(result)
      // 数据变为追加进去
      list.value.push(...result.items)
      // 页码++
      data.page++
      // 最后的长度如果为小于20
      if (result.items.length < data.pageSize) {
        finished.value = true
      }
    }
    // 监听sortParams的变化  商品列表改变刷新
    // watch(sortParams, () => {
    //   // 当页码为1的时候就要清空数据跟finished设置为false
    //   if (sortParams.page === 1) {
    //     list.value = []
    //     finished.value = false
    //     goodsList()
    //   }
    // }, { immediate: true })

    watch(sortParams, () => {
      // console.log(1)
      // 当页码为1的时候就要清空数据跟finished设置为false
      // if (sortParams.page === 1) {
      list.value = []
      finished.value = false
      // 让单前页面为第一页开始
      data = { ...data, ...sortParams, page: 1 }
      // console.log(data)
      goodsList()
      // }
    }, { immediate: true })
    return { subCate, sortParams, list, finished, loading, goodsList }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
